package org.aurora.remote;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.aurora.remote.netty.NettyRemoteClient;
import org.aurora.remote.netty.NettyRemoteServer;
import org.aurora.remote.protocol.RemoteRequest;
import org.aurora.remote.protocol.RemoteResponse;

import java.nio.charset.StandardCharsets;

public class Test {
    public static void main(String[] args) {
        System.out.println(NumberUtils.isNumber("1.1"));
        System.out.println(NumberUtils.isNumber("1"));
        System.out.println(NumberUtils.isDigits("1"));
        System.out.println(NumberUtils.isDigits("1.1"));
    }
//    public static void main(String[] args) throws InterruptedException {
//        NettyRemoteServer nettyRemoteServer = new NettyRemoteServer(8888);
//        nettyRemoteServer.registerProcessor(100, new RemoteProcessor() {
//            @Override
//            public RemoteResponse processor(RemoteRequest request) {
//                byte[] data = request.getData();
//                String s = new String(data, StandardCharsets.UTF_8);
//
//                MyRequest myRequest = JSONObject.parseObject(s, MyRequest.class);
//                System.out.println("server receive: " + myRequest.msg);
//
////                try {
////                    Thread.sleep(2001);
////                } catch (InterruptedException e) {
////                    throw new RuntimeException(e);
////                }
//                return new MyResponse("hello i am server");
//            }
//        });
//
//        NettyRemoteClient nettyRemoteClient = new NettyRemoteClient("127.0.0.1", 8888);
//
//        nettyRemoteServer.start();
//        nettyRemoteClient.start();
//
//        MyRequest myRequest = new MyRequest("hello i am client");
//        myRequest.setCommandCode(100);
//
////        syncSend(nettyRemoteClient, myRequest);
//
//        asyncSend(nettyRemoteClient, myRequest);
////
////        if (remoteResponse != null) {
////            byte[] data = remoteResponse.getData();
////            String s = new String(data, StandardCharsets.UTF_8);
////
////            MyResponse response = JSONObject.parseObject(s, MyResponse.class);
////            System.out.println("client receive: " + response.msg);
////        }
//
////        nettyRemoteClient.sendAsync(myRequest, 3000, new SendCallback() {
////            @Override
////            public void onSuccess(RemoteResponse remoteResponse) {
////                byte[] data = remoteResponse.getData();
////                String s = new String(data, StandardCharsets.UTF_8);
////                MyResponse response = JSONObject.parseObject(s, MyResponse.class);
////                System.out.println("client receive success:" + response.msg);
////            }
////
////            @Override
////            public void onFail(Throwable e) {
////                System.out.println("client receive fail:" + e);
////            }
////        });
//
//        Thread.sleep(100000000);
//    }

    static void syncSend(NettyRemoteClient nettyRemoteClient, MyRequest myRequest) {
        MyResponse remoteResponse = nettyRemoteClient.send(myRequest, 500000, MyResponse.class);
        System.out.println("client sync receive : " + remoteResponse.getMsg());
    }

    static void asyncSend(NettyRemoteClient nettyRemoteClient, MyRequest myRequest) {
        nettyRemoteClient.sendAsync(myRequest, 500000, new SendCallback<MyResponse>() {
            @Override
            public void onSuccess(MyResponse response) {
                System.out.println("client async receive : " + response.getMsg());
            }

            @Override
            public void onFail(Throwable e) {

            }
        });

    }

    private static class MyRequest extends RemoteRequest {
        private String msg;

        public MyRequest(String msg) {
            this.msg = msg;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }

    private static class MyResponse extends RemoteResponse {
        private String msg;

        public MyResponse(String msg) {
            this.msg = msg;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
